package com.primestone.cim.client.app;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.primestone.cim.client.app.window.CIMApplicationWindow;

/**
 * 
 * @author cesar.mateus
 * Jul 23, 2013 6:11:32 PM
 * MainClass.java
 * CIMClientApplication
 *
 */
public class MainClass {

	Display display;
	ProgressBar pb1;
	Table table;

	public static void main(String[] a) {
		MainClass mainClass = new MainClass();
		mainClass.open();
	}


	private void open() {
		display = new Display();
		Shell shell = new Shell(display);	
		shell.setLayout(new GridLayout());

		pb1 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
		pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		pb1.setMinimum(0);
		pb1.setMaximum(30);

		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		table.setBounds(10, 161, 964, 219);
		table.setLinesVisible(true);

		Button btnIniciar = new Button(shell, SWT.PUSH);
		btnIniciar.setImage(SWTResourceManager.getImage(CIMApplicationWindow.class, "/com/sun/java/swing/plaf/windows/icons/Computer.gif"));
		btnIniciar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.out.println("---- te lance long long proc.");
				new LongRunningOperation(display, pb1, table).start();		
			}
		});

		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}
}

class LongRunningOperation extends Thread {
	private Display display;
	private ProgressBar progressBar;
	private Table table;

	public LongRunningOperation(Display display, ProgressBar progressBar, Table table) {
		this.display = display;
		this.progressBar = progressBar;
		this.table = table;
	}

	public void run() {
		//for (int i = 0; i < 3000; i++) {
			try {
				System.out.println("a dormir un rato...");
				Thread.sleep(10000000);
				System.out.println("listo");
			} catch (InterruptedException e) {
			}
			display.asyncExec(new Runnable() {
				public void run() {
					final int j = 2;
					
					TableItem item = new TableItem(table, SWT.NONE);
					item.setText("Ticket: " + j +  " (block " + j + ").");	
					

					if (progressBar.isDisposed())
						return;
					progressBar.setSelection(progressBar.getSelection() + 1);
				}
			});
		//}
	}
}

//public void refresh(final String ticket, final String idBlock) {
//	//Display display = this.shlCimClientApplication.getDisplay();
//	Display display = this.display;
//
//	display.equals(this.shlCimClientApplication.getDisplay());
//
//	if (!(display==null || display.isDisposed())) {
//		display.syncExec(new Runnable () {
//			public void run() {
//				Table t = getTable();
//				//t.removeAll();
//				//Iterator i = table_.values().iterator();
//				//					while (i.hasNext()) {
//				//						String[] rowData = (String[]) i.next();
//				//						(new TableItem(t, SWT.NONE)).setText(rowData);
//				//					}
//
//				if (getTable().isDisposed ()) return;
//				TableItem item = new TableItem(t, SWT.NONE);
//				item.setText("Ticket: " + ticket +  " (Bloque " + idBlock + ").");
//			}
//		});
//	}
//}
//
//private void runNewThread(final String ticket, final String idBlock) {
//	Runnable r = new Runnable() {
//		@Override
//		public void run() {
//			if (getTable().isDisposed ()) return;
//			TableItem item = new TableItem(getTable(), SWT.NONE);
//			item.setText("Ticket: " + ticket +  " (Bloque " + idBlock + ").");		
//		}
//	};
//
//	if(Display.getCurrent() != null) {
//		r.run();
//	}
//	else {
//		Display.getDefault().asyncExec(r);
//	}
//}
